#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int reversePairs(vector<int>& record) {
    int n = record.size();
    vector<int> tmp(n);
    auto merge_sort = [&](auto&& merge_sort, int l, int r)->int {
        if (l >= r)  return 0;
        int mid = l + r >> 1;
        int res = merge_sort(merge_sort,l, mid) + merge_sort(merge_sort,mid + 1, r);

        int i = l, j = mid + 1;
        int k = l;
        while (i <= mid && j <= r)
        {
            if (record[i] <= record[j])  tmp[k++] = record[i++];
            else
            {
                tmp[k++] = record[j++];
                res += (mid - i + 1);
            }
        }
        while(i <= mid) tmp[k++] = record[i++];
        while(j <= r)   tmp[k++] = record[j++];

        for (i = l; i <= r; i++)   record[i] = tmp[i];
        return res;
    };
    return merge_sort(merge_sort, 0, n - 1);
}

int main()
{
    vector<int> v = { 7,5,6,4 };

    cout << reversePairs(v);
    return 0;
}